package com.JobFind.DAO.impl;

import java.util.List;

import javax.annotation.Resource;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.JobFind.DAO.ICompanyDAO;
import com.JobFind.model.TbCompany;
import com.JobFind.model.TbUser;

@Component
public class CompanyDAO implements ICompanyDAO {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@SuppressWarnings("unchecked")
	public List<TbCompany> loginCheck(String companyLoginName) throws Exception {
		List<TbCompany> result = null;
		Session session = sessionFactory.getCurrentSession();
		String hql = "from TbCompany where companyLoginName = ? and companyStatus = 1";
		Query query = session.createQuery(hql);
		query.setString(0,companyLoginName);
		result = query.list();
		return result;
	}

	public TbCompany getCompanyInfoById(Integer companyId) throws Exception {
		TbCompany result=null;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="from TbCompany where companyId = ?";
			Query query = session.createQuery(hql);
			query.setInteger(0, companyId);
			result = (TbCompany) query.list().get(0);
		} catch (Exception e) {
		  e.printStackTrace();
		  result = null;
		}
		return result;
	}

	public Integer updateCompanyInfo(TbCompany companyBean) throws Exception {
		Integer result = 1;
		try {
			Session session = sessionFactory.getCurrentSession();
			session.update(companyBean);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	public Integer modifyCompanyPwd(Integer companyId, String companyPwd)
			throws Exception {
		Integer result = 1;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="update TbCompany set companyPwd=? where companyId=?";
			Query query = session.createQuery(hql);
			query.setString(0, companyPwd);
			query.setInteger(1, companyId);
			query.executeUpdate();
		} catch (Exception e) {
		  e.printStackTrace();
		  result = 0;
		}
		return result;
	}

	public Integer updateCompanyLogo(String companyLogo,Integer companyId) throws Exception {
		Integer result = 1;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="update TbCompany set companyLogo=? where companyId=?";
			Query query = session.createQuery(hql);
			query.setString(0, companyLogo);
			query.setInteger(1, companyId);
			query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<TbCompany> getComInfoByName(String companyName)
			throws Exception {
		List<TbCompany> result = null;
		Session session = sessionFactory.getCurrentSession();
		String hql = "from TbCompany where companyName = ?";
		Query query = session.createQuery(hql);
		query.setString(0,companyName);
		result = query.list();
		return result;
	}

	public Integer addCompany(TbCompany companyBean) throws Exception {
		Integer result = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			result = (Integer) session.save(companyBean);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	public List<TbCompany> getAllUnCheckCom() throws Exception {
		List<TbCompany> result = null;
		try{
			Session session = sessionFactory.getCurrentSession();
			String hql = "from TbCompany where companyStatus = ?";
			Query query = session.createQuery(hql);
			query.setInteger(0,0);
			result = query.list();
		}catch (Exception e){
			e.printStackTrace();
			result=null;
		}
		return result;
	}

	public Integer checkCompanyInfo(Integer companyId, Integer companyStatus)
			throws Exception {
		Integer result = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "update TbCompany set companyStatus = ? where companyId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, companyStatus);
			query.setInteger(1, companyId);
			result = query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	public String getCompanyLoginNameById(Integer companyId) throws Exception {
		String result = "";
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql="select companyLoginName from TbCompany where companyId=?";
			Query query = session.createQuery(hql);
			query.setInteger(0, companyId);
			result = query.uniqueResult().toString();
			
		} catch (Exception e) {
			e.printStackTrace();
			result = "";
		}
		return result;
	}
}
